#ifndef SOBJC_COMMONS_STR_LIST_H
#define SOBJC_COMMONS_STR_LIST_H

#include <sobjc-commons/port.h>
#include <sobjc-commons/list.h>

static __inline__ char *
objc_strdup(const char *from)
{
  char *to;

  to = (char *)objc_malloc_atomic(strlen(from)+1);
  strcpy(to, from);
  return to;
}

OBJC_DEF_LIST(str_list, char *, const char *, objc_strdup, objc_free)

static __inline__ size_t
objc_str_list_len(objc_str_list_t list)
{
  size_t len = 0;

  while (list) {
    len += strlen(list->car);
    list = list->cdr;
  }
  return len;
}

static __inline__ char *
objc_str_list_join(objc_str_list_t list, const char *sep)
{
  objc_str_list_t temp;
  size_t len = 1, seplen;
  char *join;

  seplen = strlen(sep);
  temp = list;
  while (temp != NULL) {
    len += strlen(temp->car) + seplen;
    temp = temp->cdr;
  }
  len -= seplen; /* last */

  join = (char *)objc_malloc_atomic(sizeof(char) * len);
  join[0] = '\0';
  temp = list;
  while (temp != NULL) {
    join = strcat(join, temp->car);
    temp = temp->cdr;
    if (temp != NULL)
      join = strcat(join, sep);
  }

  return join;
}

static __inline__ char **
objc_str_list_to_array(objc_str_list_t list)
{
  char **ap;
  size_t i;

  ap = (char **)objc_malloc(sizeof(char *) *
                            (objc_str_list_get_size(list)+1));
  i = 0;
  while (list != NULL) {
    ap[i] = list->car;
    list = list->cdr;
    i++;
  }
  ap[i-1] = 0;
  return ap;
}

static __inline__ objc_str_list_t
objc_str_list_expand(objc_str_list_t list, objc_str_list_t expand)
{
  while (expand) {
    list = objc_str_list_add(list, expand->car);
    expand = expand->cdr;
  }
  return list;
}

#endif /* SOBJC_COMMONS_STR_LIST_H */

